﻿using common;
using System;
using System.IO;
using System.Windows.Forms;
using YNCNumberSys.db;
using cadApp = Autodesk.AutoCAD.ApplicationServices.Application;
namespace YNCNumberSys
{
    /// <summary>
    /// XSL 。
    /// </summary>
    public partial class UsmLogInForm : Form
    {
        private CommandManager commandManager = new CommandManager();
        private ConnectionManager connectionManager = new ConnectionManager();
        UserEntity m_user = null;
        public UserEntity User
        {
            get { return m_user; }
            set { m_user = value; }
        }

        public UsmLogInForm()
        {
            InitializeComponent();
            string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
            string file = Path.Combine(Path.GetDirectoryName(filePath), "config.ini");
            if (File.Exists(file))
            {
                FileIni t = new FileIni(file);
                string key=t.ReadValue("配置", "登录名");
                this.tbx_user.Text = key;
                string key2=t.ReadValue("配置", "密码");
                this.tbx_pwd.Text = EnOrDeEncryptHelper.DecryptText(key2);
                string key3=t.ReadValue("配置", "Check");
                checkBoxUsm.Checked=key3=="1"?true:false;
            }
        }

        private void btn_loadOK_Click(object sender, EventArgs e)
        {
            userDal dal = new userDal();
            try
            {
                bool rt=dal.IsUser(this.tbx_user.Text, EnOrDeEncryptHelper.EncryptText(tbx_pwd.Text));
                if (!rt)
                {
                    cadApp.ShowAlertDialog("用户名或密码错误，请重新输入！");
                }
                else
                {
                    m_user = dal.GetUserByUserCode(this.tbx_user.Text);
                    this.Hide();
                    if(this.checkBoxUsm.Checked)
                    {
                        //string file = Path.Combine(System.Windows.Forms.Application.StartupPath, "config.ini");
                        string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                        string file = Path.Combine(Path.GetDirectoryName(filePath), "config.ini");
                        if (File.Exists(file))
                        {
                            FileIni t = new FileIni(file);
                            t.WriteValue("配置", "登录名", this.tbx_user.Text);
                            t.WriteValue("配置", "密码", EnOrDeEncryptHelper.EncryptText(tbx_pwd.Text));
                            t.WriteValue("配置", "Check", checkBoxUsm.Checked?"1":"0");
                        }
                    }
                    this.DialogResult = DialogResult.OK;
                    this.Close();
                }
            }
            catch (System.Exception ex)
            {
                cadApp.ShowAlertDialog(ex.Message);
            }
            
        }
        /// <summary>
        /// 修改没有结束事件，低级错误。
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LogIn_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.Visible)
            {
                this.Hide();
                if (this.checkBoxUsm.Checked)
                {
                    //string file = Path.Combine(System.Windows.Forms.Application.StartupPath, "config.ini");
                    //string file = Path.Combine(System.Reflection.Assembly.GetExecutingAssembly().Location, "config.ini");
                    string filePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
                    string file = Path.Combine(Path.GetDirectoryName(filePath), "config.ini");
                    if (File.Exists(file))
                    {
                        FileIni t = new FileIni(file);
                        t.WriteValue("配置", "登录名", this.tbx_user.Text);
                        t.WriteValue("配置", "密码", EnOrDeEncryptHelper.EncryptText(tbx_pwd.Text));
                        t.WriteValue("配置", "Check", checkBoxUsm.Checked ? "1" : "0");
                    }
                }
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }
    }
}
